package com.lin.blog.controller;


import com.lin.blog.entity.Permission;
import com.lin.blog.service.IPermissionService;
import com.lin.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Api(tags = "Permission接口")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;


    /**
     * 通过主键ID查询Permission
     * @param id
     * @return
     */
    @ApiOperation("通过主键ID查询用Permission")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @GetMapping("/{id}")
    public Result getPermissionById(@PathVariable(value = "id",required = true) Long id){
        return permissionService.getPermission(id);
    }

    /**
     * 插入一条Permission记录
     * @param permission
     * @return
     */
    @RequiresRoles("ADMIN")
    @ApiOperation("插入一条Tags记录")
    @PostMapping
    public Result insertPermission(@RequestBody @Valid Permission permission){
        return permissionService.savePermission(permission);
    }


    /**
     * 通过主键ID删除Permission
     * @param id
     * @return
     */
    @RequiresRoles("ADMIN")
    @ApiOperation("通过主键ID删除Permission")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @DeleteMapping("/{id}")
    public Result deletePermissionById(@PathVariable(value = "id",required = true) Long id){
        return permissionService.removePermission(id);
    }

    /**
     * 通过主键ID更新Permission
     * @param id
     * @param permission
     * @return
     */
    @RequiresRoles("ADMIN")
    @ApiOperation("通过主键ID更新Permission")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @PutMapping("/{id}")
    public Result updateTags(@PathVariable(value = "id",required = true) Long id,@RequestBody Permission permission){
        return permissionService.updateTags(id,permission);
    }


    /**
     * 分页获取PermissionList
     * @return
     */
    @ApiOperation("分页获取PermissionList")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条")})
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result getPermissionList(@PathVariable("pageNum")  Long pageNum,
                              @PathVariable("pageSize")  Long pageSize){
        return permissionService.listPermission(pageNum,pageSize);

    }


    /**
     * 获取PermissionList
     * @return
     */
    @ApiOperation("获取PermissionList")
    @GetMapping("/list")
    public Result getPermissionList(){
        return permissionService.listPermission();

    }
}
